home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Includes / UGeometry.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  14.3 KB  |  440 lines  |  [TEXT/MPS ]

  1. // UGeometry.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __UGEOMETRY__
  5. #define __UGEOMETRY__
  6.  
  7. // MacApp
  8.  
  9. #ifndef __GEOMETRY__
  10. #include "Geometry.h"
  11. #endif
  12.  
  13. // for CCharString types
  14. #ifndef __PASCALSTRING__
  15. #include "PascalString.h"
  16. #endif
  17.  
  18. // Toolbox
  19.  
  20. #ifndef __TYPES__
  21. #include <Types.h>
  22. #endif
  23.  
  24.  
  25. //----------------------------------------------------------------------------------------
  26. // Constants
  27. //----------------------------------------------------------------------------------------
  28.  
  29. const short kMaxCoord = 30000;
  30.  
  31. //----------------------------------------------------------------------------------------
  32. // Forward and external typedefs and class declarations.
  33. //----------------------------------------------------------------------------------------
  34.  
  35. class VPoint;
  36. class VRect;
  37.  
  38. typedef class VPoint *VPointPtr;
  39.  
  40. typedef class VRect *VRectPtr;
  41.  
  42.  
  43. //----------------------------------------------------------------------------------------
  44. // VCoordinates are 32 bits as compared to QD Coordinates which are 16 bits
  45. //----------------------------------------------------------------------------------------
  46.  
  47. typedef long VCoordinate;
  48.  
  49.  
  50. //----------------------------------------------------------------------------------------
  51. // VPoint: is a toolbox compatible class for the old struct VPoint. It is bitwise
  52. // compatible with the old struct because it contains no virtual functions.
  53. //----------------------------------------------------------------------------------------
  54.  
  55. class VPoint
  56. {
  57. public:
  58.     VCoordinate v;
  59.     VCoordinate h;
  60.  
  61.     //------------------------------------------------------------------------------------
  62.     // Constructors
  63.     //------------------------------------------------------------------------------------
  64.  
  65.     inline VPoint()
  66.     { }
  67.     
  68.     inline VPoint(VCoordinate iH, VCoordinate iV) :
  69.         v(iV),
  70.         h(iH)
  71.     { }
  72.           
  73.     inline VPoint(const CPoint& pt) :
  74.         v(pt.v),
  75.         h(pt.h)
  76.     { }
  77.     
  78. #if qPowerPC
  79.     // copy constructor
  80.     inline VPoint(const VPoint& pt)
  81.     { *(double*)this = *(const double*)&pt; }
  82. #endif
  83.  
  84.     //------------------------------------------------------------------------------------
  85.     // Copy and conversion operator methods
  86.     //------------------------------------------------------------------------------------
  87.  
  88. #if qPowerPC
  89.     inline VPoint& operator=(const VPoint& pt)
  90.     { *(double*)this = *(const double*)&pt; return *this; }
  91.         // cheat, we know that we're eight bytes long
  92. #endif
  93.         
  94. #if qDebug
  95.         // Conversion operator, for converting VPoint to a textual representation of a
  96.         // VPoint. "VPoint(h,v)".
  97.     inline operator const char*() const
  98.     {
  99.         char textPoint[64];
  100.         sprintf (textPoint, "VPoint(%d, %d)", h, v);
  101.         return CChar63(textPoint);
  102.     }
  103. #endif
  104.     
  105.     inline CPoint AsPoint() const
  106.     { return CPoint((short) h, (short) v); }
  107.  
  108.         // Creates a CPoint by casting the VCoordinate values to short values. In other
  109.         // words, it throws away the top 16 bits of each long word value. If the values
  110.         // don't fit within 16 bits you will get strange results (i.e. 32769 -> 1). 
  111.         // Danger: Don't use this in TView methods for converting from view space to QD
  112.         // space. Instead, use TView::ViewToQDPt()!
  113.     
  114.     CPoint ToPoint() const;
  115.         // Creates a CPoint by limiting the VCoordinate values to +/- 32K and casting
  116.         // them to short values. This produces reasonable results if the values were
  117.         // more than 16 bits (i.e. 32769 -> 32767). 
  118.         // Danger: Don't use this in TView methods for converting from view space to QD
  119.         // space. Instead, use TView::ViewToQDPt()!
  120.     
  121.     //------------------------------------------------------------------------------------
  122.     // Selector operators, two are needed one for operating on const VPoints and the other
  123.     // on non-const VPoints.
  124.     // Because of inlining, constant sel parameters resolve at compile time.
  125.     //------------------------------------------------------------------------------------
  126.     
  127.     inline VCoordinate& operator[](VHSelect sel)
  128.     { return (sel == vSel) ? v : h; }
  129.         // For non-const VPoint
  130.         
  131.     inline const VCoordinate& operator[](VHSelect sel) const
  132.     { return (sel == vSel) ? v : h; }
  133.         // For const VPoint
  134.  
  135.  
  136.     //------------------------------------------------------------------------------------
  137.     // Arithmetic operators
  138.     //------------------------------------------------------------------------------------
  139.  
  140.     VPoint operator+(const VPoint& pt) const;
  141.         // c = a + b    (a,b and c are VPoints)
  142.         
  143.     VPoint operator-(const VPoint& pt) const;
  144.         // c = a - b    (a,b and c are VPoints)
  145.  
  146.     VPoint operator-() const;
  147.         // c = -a        negate a
  148.  
  149.     VPoint& operator+=(const VPoint& pt);
  150.         // c += a        (a and c are VPoints, and
  151.         //                a reference to a is returned,
  152.         //                so these can be strung together)
  153.         
  154.     VPoint& operator-=(const VPoint& pt);
  155.         // c -= a        ( ditto )
  156.         
  157.  
  158.     //------------------------------------------------------------------------------------
  159.     // Relational operators, simply apply the relation to both coordinates if condition
  160.     // holds for both coordinates then holds for the CPoint. Exception is != if either
  161.     // coordinate is not equal then points are not equal.
  162.     //------------------------------------------------------------------------------------
  163.  
  164.     Boolean operator!=(const VPoint& pt) const;
  165.     
  166.     Boolean operator==(const VPoint& pt) const;
  167.     
  168.     Boolean operator>(const VPoint& pt) const;
  169.     
  170.     Boolean operator<(const VPoint& pt) const;
  171.     
  172.     Boolean operator>=(const VPoint& pt) const;
  173.     
  174.     Boolean operator<=(const VPoint& pt) const;
  175.     
  176.     
  177.     void ConstrainTo(const VRect& rt);
  178.         // If the VPoint is not already inside the rectangle move the VPoint to the nearest
  179.         // edge.
  180.         
  181. protected:
  182.  
  183.     inline VCoordinate Min(const VCoordinate a,
  184.               const VCoordinate b) const
  185.     { return a < b ? a : b; }
  186.               
  187.     inline VCoordinate Max(const VCoordinate a,
  188.               const VCoordinate b) const
  189.     { return a > b ? a : b; }
  190.               
  191.     inline VCoordinate MinMax(VCoordinate MinVal,
  192.                        VCoordinate expression,
  193.                        VCoordinate MaxVal) const
  194.     { return Min(Max(expression, MinVal), MaxVal); }
  195.     // Returns the bounded minimum and maximum 
  196. };
  197.  
  198.  
  199. //----------------------------------------------------------------------------------------
  200. // VRect is a toolbox compatible class for the old struct VRect. It is bitwise compatible
  201. // with the old struct because it contains no virtual functions.
  202. //----------------------------------------------------------------------------------------
  203.  
  204. class VRect
  205. {
  206. public:
  207.     VCoordinate top;
  208.     VCoordinate left;
  209.     VCoordinate bottom;
  210.     VCoordinate right;
  211.  
  212.     //------------------------------------------------------------------------------------
  213.     // VRect constructors
  214.     //------------------------------------------------------------------------------------
  215.  
  216.     inline VRect()
  217.     { }
  218.     
  219.     inline VRect(VCoordinate iLeft, VCoordinate iTop, VCoordinate iRight, VCoordinate iBottom) :
  220.         top(iTop),
  221.         left(iLeft),
  222.         bottom(iBottom),
  223.         right(iRight)
  224.     { }
  225.  
  226.          
  227.     inline VRect(const VPoint& topLeftPt, const VPoint& botRightPt)
  228.     {
  229.         *(VPoint*)&this->top = *(const VPoint*)&topLeftPt;
  230.         *(VPoint*)&this->bottom = *(const VPoint*)&botRightPt;
  231.     }
  232.          
  233.     inline VRect(const CRect& aRect) :
  234.         top(aRect.top),
  235.         left(aRect.left),
  236.         bottom(aRect.bottom),
  237.         right(aRect.right)
  238.     { }
  239.     
  240.     inline VRect(const Rect& aRect) :
  241.         top(aRect.top),
  242.         left(aRect.left),
  243.         bottom(aRect.bottom),
  244.         right(aRect.right)
  245.     { }
  246.  
  247. #if qPowerPC
  248.     // copy constructor
  249.     inline VRect(const VRect& aVRect)
  250.     {
  251.         *(VPoint*)&this->top = *(const VPoint*)&aVRect.top;
  252.         *(VPoint*)&this->bottom = *(const VPoint*)&aVRect.bottom;
  253.     }
  254. #endif        
  255.  
  256.     //------------------------------------------------------------------------------------
  257.     // Copy and conversion operator methods
  258.     //------------------------------------------------------------------------------------
  259.     
  260. #if qPowerPC
  261.     inline VRect& operator=(const VRect& aVRect)
  262.     {
  263.         *(VPoint*)&this->top = *(const VPoint*)&aVRect.top;
  264.         *(VPoint*)&this->bottom = *(const VPoint*)&aVRect.bottom;
  265.         return *this;
  266.     }
  267. #endif        
  268.  
  269.     
  270. #if qDebug
  271.         // Conversion operator, for converting VRect to a textual representation of a
  272.         // VRect. "VRect(top,righ,bottom,left)".
  273.     inline operator const char*() const
  274.     {
  275.         char textRect[64];
  276.         sprintf (textRect, "VRect(%d, %d, %d, %d)", left, top, right, bottom);
  277.         return CChar63(textRect);
  278.     }
  279. #endif        
  280.     
  281.     inline CRect AsRect() const
  282.     { return CRect((short) left, (short) top, (short) right, (short) bottom); }
  283.         // Creates a CRect by casting the VCoordinate values to short values. In other
  284.         // words, it throws away the top 16 bits of each long word value. If the values
  285.         // don't fit within 16 bits you will get strange results (i.e. 32769 -> 1). 
  286.         // Danger: Don't use this in TView methods for converting from view space to QD
  287.         // space. Instead, use TView::ViewToQDRect()!
  288.  
  289.     CRect ToRect() const;
  290.         // Creates a CRect by limiting the VCoordinate values to +/- 32K and casting
  291.         // them to short values. This produces reasonable results if the values were
  292.         // more than 16 bits (i.e. 32769 -> 32767). 
  293.         // Danger: Don't use this in TView methods for converting from view space to QD
  294.         // space. Instead, use TView::ViewToQDRect()!
  295.     
  296.     
  297.     
  298.     //------------------------------------------------------------------------------------
  299.     // VPoint selectors for VRect. One for operating on const VRects and one for non-const
  300.     // VRects.
  301.     // Because of inlining, constant sel parameters resolve at compile time.
  302.     //------------------------------------------------------------------------------------
  303.  
  304.     inline VPoint& operator[](PointSelector sel)
  305.     { return (sel == topLeft) ? (*((VPoint *) &top)) : (*((VPoint *) &bottom)); }
  306.         // Selector for non-const VRects
  307.         
  308.     inline const VPoint& operator[](PointSelector sel) const
  309.     { return (sel == topLeft) ? (*((const VPoint *) &top)) : (*((const VPoint *) &bottom)); }
  310.         // Selector for const VRects
  311.  
  312.  
  313.     //------------------------------------------------------------------------------------
  314.     // Operators for adding and subtracting one VRect from to/from another.
  315.     //------------------------------------------------------------------------------------
  316.  
  317.     VRect operator+(const VRect& rt) const;
  318.     
  319.     VRect operator-(const VRect& rt) const;
  320.     
  321.     VRect& operator+=(const VRect& rt);
  322.     
  323.     VRect& operator-=(const VRect& rt);
  324.  
  325.  
  326.     //------------------------------------------------------------------------------------
  327.     // Operators overloaded for adding and subtracting some increment to/from each CPoint
  328.     // along both axis. Very convenient for translating VRects. These take a CPoint and
  329.     // since VPoint has a constructor that takes two VCoordinates the VRect windowVRect
  330.     // can be translated 100 pixels in the positive y direction by the statement:
  331.     //
  332.     //    windowVRect = windowVRect + VPoint (0, 100);    or
  333.     //     windowVRect += VPoint (0, 100);
  334.     //------------------------------------------------------------------------------------
  335.  
  336.     VRect operator+(const VPoint& pt) const;
  337.     
  338.     VRect operator-(const VPoint& pt) const;
  339.     
  340.     VRect operator-() const;
  341.         // Negation
  342.     
  343.     VRect& operator+=(const VPoint& pt);
  344.     
  345.     VRect& operator-=(const VPoint& pt);
  346.     
  347.     
  348.     //------------------------------------------------------------------------------------
  349.     // Miscellaneous methods
  350.     //------------------------------------------------------------------------------------
  351.  
  352.     Boolean operator==(const VRect& rt) const;
  353.         // Returns true if all four coordinates are equal
  354.     
  355.     Boolean operator!=(const VRect& rt) const;
  356.         // Returns true if at least one of the four coordinates are not equal.
  357.  
  358.     VRect operator &(const VRect& rt) const;
  359.         // Returns the intersection of 'this' and 'rt'.
  360.     
  361.     VRect operator |(const VRect& rt) const;
  362.         // Returns the union of 'this' and 'rt'. Actually not the true mathematical union
  363.         // but the smallest rectangle that bounds both 'this' and 'rt'.
  364.  
  365.     VRect& Inset(const VPoint& delta);
  366.         // Inset a VRect by VPoint.
  367.     
  368.     Boolean Valid() const;
  369.         // Returns true if a valid rectangle (left < right and top < bottom).
  370.         
  371.     void Validate();
  372.         // Validate rectagle by switching the points if right-bottom is above or to the
  373.         // left of left-top.
  374.     
  375.     Boolean Empty() const;
  376.         // Return true if the VRect is empty (right - left = 0 and bottom - top = 0)
  377.     
  378.     
  379.     VCoordinate GetLength(VHSelect sel) const;
  380.         // Returns the length of a VRect in a given dimension.
  381.         
  382.     VPoint GetSize() const;
  383.         // Returns the size of a VRect in a VPoint.
  384.     
  385.     Boolean Contains(const VPoint& pt) const;
  386.         // Returns true if 'pt' is contained in the rectangle 'this'
  387.         
  388.     Boolean Contains(const VRect& rt) const;
  389.         // Return true if 'rt' is contained in the rectangle 'this'
  390.  
  391. protected:
  392.  
  393.     inline VCoordinate Min(const VCoordinate a,
  394.               const VCoordinate b) const
  395.     { return a < b ? a : b; }
  396.               
  397.     inline VCoordinate Max(const VCoordinate a,
  398.               const VCoordinate b) const
  399.     { return a > b ? a : b; }
  400.               
  401.     inline VCoordinate MinMax(VCoordinate MinVal,
  402.                        VCoordinate expression,
  403.                        VCoordinate MaxVal) const
  404.     { return Min(Max(expression, MinVal), MaxVal); }
  405.     // Returns the bounded minimum and maximum 
  406. };
  407.  
  408.  
  409. //========================================================================================
  410. // The following global routines were originally for Pascal compatibility. They are basically
  411. // wrappers that call back into the methods in VRect and VPoint. C++ programs should use
  412. // the methods in VRect and VPoint to avoid the additional function call overhead.
  413. //
  414. // Left in for compatibility.
  415. //========================================================================================
  416.  
  417. void PtToVPt(const CPoint thePt, VPoint& theVPt);
  418. CPoint VPtToPt(const VPoint& theVPt);
  419. void RectToVRect(const CRect& theRect, VRect& theVRect);
  420. void VRectToRect(const VRect& theVRect, CRect& theRect);
  421. void AddVPt(const VPoint& srcVPt, VPoint& dstVPt);
  422. void SubVPt(const VPoint& srcVPt, VPoint& dstVPt);
  423. void SetVPt(VPoint& vPt, VCoordinate h, VCoordinate v);
  424. Boolean EqualVPt(const VPoint& aVPt, const VPoint& bVPt);
  425. void SetVRect(VRect& vRt,
  426.               VCoordinate left, VCoordinate top,
  427.               VCoordinate right, VCoordinate bottom);
  428. void OffsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv);
  429. void InsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv);;
  430. void Pt2VRect(const VPoint& theTopLeft, const VPoint& theBotRight, VRect& vRt);
  431. Boolean PtInVRect(const VPoint& vPt, const VRect& vRt);
  432. Boolean EmptyVRect(const VRect& vRt);
  433. Boolean EqualVRect(const VRect& aVRt, const VRect& bVRt);
  434. VCoordinate LengthVRect(const VRect& vRt, VHSelect whichDim);
  435. void PinVRect(const VRect& vRt, VPoint& vPt);
  436. Boolean SectVRect(const VRect& src1, const VRect& src2, VRect& dst);
  437. void UnionVRect(const VRect& src1, const VRect& src2, VRect& dst);
  438.  
  439. #endif
  440.